package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author QingMeng
 * @date 2024-10-23
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private IVmTypeService iVmTypeService;
    @Autowired
    private INodeService iNodeService;
    @Autowired
    private IChannelService iChannelService;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1、添加设备信息
        //1.1、添加设备编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //1.2、添加设备容量
        VmType vmType = iVmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        //1.3、添加区域信息
        Node node = iNodeService.selectNodeById(vendingMachine.getNodeId());
        //1.3.1、拷贝区域信息
        BeanUtil.copyProperties(node,vendingMachine,"id");//区域id，合作商id
        vendingMachine.setBusinessType(node.getBusinessAreaType());//商圈类型
        vendingMachine.setAddr(node.getAddress());//地址
        //1.4、设置设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);//0-未部署
        //1，5、设置创建时间和修改时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());//创建时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());//修改时间
        //1.6、保存
        int Result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        //2、添加货道信息
        //2.1、定义一个集合用来存储货道对象
        List<Channel> channels = new ArrayList<>();
        //2.2、双层for循环
        for (int i = 1; i <= vmType.getVmRow() ; i++) { // 外部行
            for (int j = 1; j <= vmType.getVmCol() ; j++) { // 内部列
                //2.3、定义一个新的货道对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);//货道编号
                channel.setVmId(vendingMachine.getId());//售货机id
                channel.setInnerCode(innerCode);//售货机编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());//货道最大容量
                channel.setCreateTime(DateUtils.getNowDate());//创建时间
                channel.setUpdateTime(DateUtils.getNowDate());//修改时间
                //2.4、保存货道信息
                // iChannelService.insertChannel(channel);
                //将对象保存到集合中
                channels.add(channel);
            }
        }
        //2.4、批量新增货道数据
        iChannelService.batchInsertChannel(channels);
        return Result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
       //设置当Nodeid不为空时才添加区域，合作商，详细地址等信息
        if(vendingMachine.getNodeId()!=null){
            //查询点位表 补充区域，合作商，详细地址等信息
            Node node = iNodeService.selectNodeById(vendingMachine.getNodeId());
            BeanUtil.copyProperties(node,vendingMachine,"id");//商圈类型，区域，合作商
            vendingMachine.setAddr(node.getAddress());//详细地址
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());//修改时间
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 根据售货机编码查询售货机信息
     * @param innerCode
     * @return VendingMachine
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }
}
